package com.naiterui.ehp.bs.consult.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.recommend.RecomPrescriptionBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.consult.dto.VideoConsultDTO;
import com.naiterui.ehp.bs.consult.service.IVideoConsultService;
import com.naiterui.ehp.bs.consult.vo.DoctorConsultDetailVO;
import com.naiterui.ehp.bs.consult.vo.DoctorConsultListVO;
import com.naiterui.ehp.bs.consult.vo.PatientConsultRecordVO;
import com.naiterui.ehp.bs.consult.vo.RefusalReasonVO;
import com.naiterui.ehp.bs.consult.vo.UserInfoVO;
import com.naiterui.ehp.bs.consult.vo.VideoConsultConfigVO;
import com.naiterui.ehp.bs.consult.vo.VideoConsultMessageListVO;
import com.naiterui.ehp.bs.consult.vo.VideoConsultRoomConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 视频问诊
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021-02-26 9:39
 **/
@Api(tags = SwaggerTag.VIDEO + SwaggerTag.VIDEO_CONSULT)
@RestController
@RequiredArgsConstructor
@RequestMapping("/video/consult")
@Slf4j
@Validated
public class VideoConsultController {

    private final IVideoConsultService videoConsultService;

    @PostMapping("/accept")
    @ApiOperation(value = "医生接受问诊和进入房间")
    public ResponseVO<List<VideoConsultMessageListVO>> accept(VideoConsultDTO videoConsultDTO) throws BusinessException {
        return PublicService.returnResponseVO(this.videoConsultService.accept(videoConsultDTO));
    }

    @PostMapping("/refuse")
    @ApiOperation(value = "医生拒绝问诊")
    public ResponseVO<List> refuse(VideoConsultDTO videoConsultDTO) throws BusinessException {
        this.videoConsultService.refuse(videoConsultDTO);
        return PublicService.returnResponseVO(Collections.emptyList());
    }

    @PostMapping("/sendGroupMsg")
    @ApiOperation(value = "发送群组消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "房间号", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "isDoctorSend", value = "是否医生发送：1.医生 0.患者", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "file", value = "图片", required = true, dataType = "file", paramType = "add")
    })
    public ResponseVO<List> sendGroupMsg(@NotNull(message = "房间号不能为空") Integer roomId,
                                         @NotNull(message = "发送标识不能为空") Integer isDoctorSend,
                                         @NotEmpty(message = "用户id不能为空") String userId,
                                         @NotNull(message = "上传文件不能为null") MultipartFile file) throws BusinessException {
        Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        if (patientId != null) {
            userId = "patient-" + patientId;
        }
        log.info("send group msg roomId:{}, isDoctorSend:{}, userId:{}", roomId, isDoctorSend, userId);
        this.videoConsultService.sendGroupMsg(roomId, isDoctorSend, userId, file);
        return PublicService.returnResponseVO(Collections.emptyList());
    }

    @GetMapping("/getImageList")
    @ApiOperation(value = "获取图片列表")
    @ApiImplicitParam(name = "videoConsultId", value = "视频咨询id", required = true, dataType = "long", paramType = "query")
    public ResponseVO<List<String>> getImageList(Long videoConsultId) {
        return PublicService.returnResponseVO(this.videoConsultService.getImageList(videoConsultId));
    }

    @RequestMapping("/finish")
    @ApiOperation(value = "结束问诊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "房间号", required = true, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "diagnosis", value = "诊断信息（多个诊断用,隔开）", dataType = "string", paramType = "form")})
    public ResponseVO<List> finish(@NotNull(message = "房间号不能为空") @RequestParam Integer roomId, String diagnosis) {
        this.videoConsultService.finish(roomId, diagnosis);
        return PublicService.returnResponseVO(Collections.emptyList());
    }

    @PostMapping("/upload")
    @ApiOperation(value = "图片上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "房间号", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "file", value = "图片", required = true, dataType = "file", paramType = "add")
    })
    public ResponseVO<String> uploadFile(@NotEmpty(message = "房间号不能为空") String roomId,
                                         @NotNull(message = "上传文件不能为null") MultipartFile file) throws IOException {
        return PublicService.returnResponseVO(this.videoConsultService.uploadFile(roomId, file));
    }

    @GetMapping("/getAppIdAndKey")
    @ApiOperation(value = "获取appId和key")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoConsultId", value = "视频问诊id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "string", paramType = "query")})
    public ResponseVO<VideoConsultConfigVO> getAppIdAndKey(Long videoConsultId, String userId) throws BusinessException {
        Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        log.info("getAppIdAndKey patientId:{}", patientId);
        if (patientId != null) {
            userId = "patient-" + patientId;
        }
        return PublicService.returnResponseVO(this.videoConsultService.getAppIdAndKey(videoConsultId, userId));
    }

    @GetMapping("/patient/consult/record")
    @ApiOperation(value = "患者咨询记录", tags = {SwaggerTags.WX_VIDEO_CONSULT})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", dataType = "string", paramType = "query")})
    public ResponseVO<PageVO<PatientConsultRecordVO>> patientConsultRecord(Long doctorId, PageParamVO pageParamVO) {
        Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        return PublicService.returnResponseVO(videoConsultService.patientConsultRecord(patientId, doctorId, pageParamVO));
    }

    @GetMapping("/doctor/consult/list")
    @ApiOperation(value = "医生接诊列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "问诊状态", dataType = "int", paramType = "query")})
    public ResponseVO<PageVO<DoctorConsultListVO>> doctorConsultList(Long doctorId, Integer status, PageParamVO pageParamVO) throws BusinessException {
        if (doctorId == null) {
            doctorId = (Long) ResourceHolder.getResource(SettingConfig.DOCTOR_ID);
        }
        return PublicService.returnResponseVO(videoConsultService.doctorConsultList(doctorId, status, pageParamVO.getPageable()));
    }

    @GetMapping("/to/treat/num")
    @ApiOperation(value = "医生待接诊数量")
    @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")
    public ResponseVO<Integer> toTreatNum(Long doctorId) {
        if (doctorId == null) {
            doctorId = (Long) ResourceHolder.getResource(SettingConfig.DOCTOR_ID);
        }
        return PublicService.returnResponseVO(videoConsultService.toTreatNum(doctorId));
    }

    @GetMapping("/refusal/reason/list")
    @ApiOperation(value = "拒诊原因")
    public ResponseVO<List<RefusalReasonVO>> refusalReason() {
        return PublicService.returnResponseVO(videoConsultService.refusalReasonList());
    }

    @GetMapping("/doctor/consult/detail")
    @ApiOperation(value = "医生接诊详情")
    @ApiImplicitParam(name = "id", value = "咨询id", required = true, dataType = "long", paramType = "query")
    public ResponseVO<DoctorConsultDetailVO> doctorConsultDetail(Long id) throws BusinessException {
        return PublicService.returnResponseVO(videoConsultService.doctorConsultDetail(id));
    }

    @GetMapping("/patient/consult/record/test")
    @ApiOperation(value = "患者记录测试用接口（进行中和中断状态的信息）")
    public ResponseVO<PageVO<PatientConsultRecordVO>> patientConsultRecordTest(Integer status, PageParamVO pageParamVO) {
        Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        return PublicService.returnResponseVO(videoConsultService.patientConsultRecordTest(patientId, pageParamVO));
    }

    @PostMapping("/inRoom")
    @ApiOperation(value = "进入房间")
    public ResponseVO<List<VideoConsultMessageListVO>> inRoom(Long videoConsultId) throws BusinessException {
        return PublicService.returnResponseVO(videoConsultService.inRoom(videoConsultId));
    }

    @GetMapping("/room/config")
    @ApiOperation(value = "获取视频咨询的配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "房间id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "string", paramType = "query")})
    public ResponseVO<VideoConsultRoomConfigVO> videoConsultRoomConfig(@NotNull(message = "房间id不可为空") Integer roomId, String userId) throws BusinessException {
        Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        if (patientId != null) {
            userId = "patient-" + patientId;
        }
        return PublicService.returnResponseVO(videoConsultService.videoConsultRoomConfig(roomId, userId));
    }

    @GetMapping("/user/info")
    @ApiOperation(value = "根据userId获取用户信息")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "string", paramType = "query")
    public ResponseVO<UserInfoVO> getUserInfo(@NotNull(message = "用户id不可为空") String userId, Integer roomId)
        throws BusinessException {
        return PublicService.returnResponseVO(videoConsultService.getUserInfo(userId, roomId));
    }

    @GetMapping("/case/pdf")
    @ApiOperation(value = "查看病例")
    public ResponseVO<String> getCasePdf(Long videoConsultId) throws BusinessException {
        log.info("查看病例， videoConsultId:{}", videoConsultId);
        return PublicService.returnResponseVO(videoConsultService.getCasePdf(videoConsultId));
    }

    @GetMapping("/recom/list")
    @ApiOperation(value = "查看处方列表")
    public ResponseVO<List<RecomPrescriptionBO>> recomList(Long videoConsultId) throws BusinessException {
        log.info("查看处方列表， videoConsultId:{}", videoConsultId);
        return PublicService.returnResponseVO(videoConsultService.recomList(videoConsultId));
    }

    @PostMapping("/leave")
    @ApiOperation(value = "暂时离开")
    @ApiImplicitParam(name = "roomId", value = "房间号", required = true, dataType = "int", paramType = "form")
    public ResponseVO<List> leave(@NotNull(message = "房间号不能为空") @RequestParam Integer roomId,
        @NotNull(message = "医生Id不能为空") @RequestParam Long doctorId) {
        this.videoConsultService.leave(roomId, doctorId);
        return PublicService.returnResponseVO(Collections.emptyList());
    }
}